﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IPC.Client
{
    public class NamedPipeThread
    {
        string PipeName { get; set; }
        public NamedPipeThread(string pipename)
        {
            PipeName = pipename;
        }
        public delegate void Send(NamedPipeClient client);
        public Send? SendAction { get; set; }
        public Task Run()
        {
            return Task.Run(() =>
            {
                var client = new NamedPipeClient();
                System.Runtime.Loader.AssemblyLoadContext.Default.Unloading += (e) =>
                {
                    client.Send("Exit");
                };
                using (var pipe = client.CreatPipeClient(PipeName))
                {
                    pipe.Connect(5000);

                    while (true)
                    {
                        SendAction?.Invoke(client);
                    }
                }
            });
        }
    }
}
